package com.wangchao.administrator.zenmechi.ui.fragment.buyFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.customview.SpaceItemDecoration;
import com.wangchao.administrator.zenmechi.ui.fragment.cookeractivity.CookerActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/17.
 */
public class BuyFragment extends Fragment implements BuyContract.View, View.OnClickListener {

    @BindView(R.id.tv_top)
    TextView tvTop;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.tv_goShopping)
    TextView tvGoShopping;
    @BindView(R.id.ll_buy)
    LinearLayout llBuy;
    @BindView(R.id.rlv_shopping)
    RecyclerView rlvShopping;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.rl_body_buy)
    RelativeLayout rlBodyBuy;
    private View view;
    private buyPresenter presenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_buy, container, false);
        ButterKnife.bind(this, view);
        presenter = new buyPresenter(this);
        presenter.getData("name", getActivity());
        presenter.getData("name", getActivity());
        tvAdd.setOnClickListener(this);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getData("name", getActivity());
    }

    @Override
    public void buySuccess(adapterBuy adapter) {
        if (adapter != null) {
            llBuy.setVisibility(View.GONE);
            rlBodyBuy.setVisibility(View.VISIBLE);
            rlvShopping.setLayoutManager(new LinearLayoutManager(getActivity()));
            rlvShopping.addItemDecoration(new SpaceItemDecoration(5));
            rlvShopping.setAdapter(adapter);
            adapter.setmOnItemClickListener(new adapterBuy.onItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {

                }

                @Override
                public void onItemLongClick(View view, int position) {

                }

                @Override
                public void onAddClick(View view, int position) {
                    presenter.setData(position,0);
                   presenter.getData("name",getActivity());
                }

                @Override
                public void onMinusClick(View view, int position) {
                    presenter.setData(position,1);
                    presenter.getData("name",getActivity());
                }
            });
        }else{
            rlBodyBuy.setVisibility(View.GONE);
            llBuy.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void buyFailed(String msg) {
        //Toast.makeText(getActivity(), "网络获取数据失败" + msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_add:
              //  presenter.addData("怎么吃果园","http://image.zenmechi.cc/img/3/77/d0/377d057e398db7489b.jpg","苹果","11.00元/500g",2);
                Intent addIntent=new Intent(getActivity(), CookerActivity.class);
                startActivity(addIntent);
                break;

        }
    }
}
